﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;
using YJKRevitFamilyDataBase;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Designer;
using YJKExcelFormTool.Forms;

namespace YJKExcelFormTool
{
	public class TableInterface
	{
		public static Result DrawGraphPaperDirectory(ExternalCommandData cmdData, ref string message)
		{
			Result result;
			try
			{
				result = new GraphPaperDirectoryDesigner(cmdData).Execute(ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result DrawDoorWindowDetailedStatement(ExternalCommandData cmdData, ref string message)
		{
			Result result;
			try
			{
				result = new DoorWindowDetailedStatementDesigner(cmdData).Execute(ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

        public static Result DrawDoorWindowLegend(ExternalCommandData cmdData, WindowHandle hWndRevit, ref string message)
		{
			Result result;
			try
			{
				result = new DrawDoorWindowLegendControler(cmdData).Create(hWndRevit, ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

        public static Result RefreshDoorWindowLegend(ExternalCommandData cmdData, WindowHandle hWndRevit, List<Group> lstUseGroups, ref string message)
		{
			Result result;
			try
			{
				result = new DrawDoorWindowLegendControler(cmdData).Refresh(hWndRevit, lstUseGroups, ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result ImportExcelTable(ExternalCommandData cmdData, ref string message)
		{
			Result result;
			try
			{
				result = new ImportExcelTableDesigner(cmdData).Execute(ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result DrawSingleDoorWindowLegend(ExternalCommandData cmdData, ref string message)
		{
			if ((int)cmdData.Application.ActiveUIDocument.Document.ActiveView.ViewType != 1)
			{
				YJKMessageBox.Show("请切换到楼层平面视图下运行此命令！");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			LegendSettingForm legendSettingForm = new LegendSettingForm(cmdData);
			string message2 = null;
			DrawDoorWindowLegendControler drawDoorWindowLegendControler = new DrawDoorWindowLegendControler(cmdData);
			int selectedViewType = legendSettingForm.SelectedViewType;
			bool isDoor = legendSettingForm.IsDoor;
			int selectedFamilyIndex = legendSettingForm.SelectedFamilyIndex;
			int selectedSymbolIndex = legendSettingForm.SelectedSymbolIndex;
			for (;;)
			{
				legendSettingForm.SelectedViewType = selectedViewType;
				legendSettingForm.IsDoor = isDoor;
				legendSettingForm.SelectedFamilyIndex = selectedFamilyIndex;
				legendSettingForm.SelectedSymbolIndex = selectedSymbolIndex;
				if (legendSettingForm.ShowDialog() != DialogResult.OK)
				{
					break;
				}
				selectedViewType = legendSettingForm.SelectedViewType;
				isDoor = legendSettingForm.IsDoor;
				selectedFamilyIndex = legendSettingForm.SelectedFamilyIndex;
				selectedSymbolIndex = legendSettingForm.SelectedSymbolIndex;
				FamilySymbol symbol = legendSettingForm.SelectedSymbol();
				Result result = drawDoorWindowLegendControler.CreateSingleLegend(symbol, selectedViewType, legendSettingForm.ZoomScale, ref message);
				if (result == Autodesk.Revit.UI.Result.Cancelled)
				{
					break;
				}
				if (result == Autodesk.Revit.UI.Result.Failed)
				{
					YJKMessageBox.Show(message2);
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result AddMaterialTableProgram(ExternalCommandData cmdData, SubjectType subjectType, MaterialProgramInfos._Tag_TemplateType templateType, ref string message)
		{
			Result result;
			try
			{
				result = new MaterialTableDesigner(cmdData).Setting(ref message, subjectType, templateType, "");
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result EditMaterialTableProgram(ExternalCommandData cmdData, string programName, string programId, SubjectType subjectType, MaterialProgramInfos._Tag_TemplateType templateType, ref string message)
		{
			Result result;
			try
			{
				result = new MaterialTableDesigner(cmdData).Setting(ref message, subjectType, templateType, programId);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result DrawMaterialTable(ExternalCommandData cmdData, string programName, string programId, ImportMaterialTableInfos importTableInfos, ref string message)
		{
			Result result;
			try
			{
				result = new MaterialTableDesigner(cmdData).Drawing(programId, importTableInfos, ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result ExportMaterialTable(ExternalCommandData cmdData, string programName, string programId, ImportMaterialTableInfos importTableInfos, ref string message)
		{
			Result result;
			try
			{
				result = new MaterialTableDesigner(cmdData).Export(programId, importTableInfos, ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result DrawGeneralTable(ExternalCommandData cmdData, TableInfos tableInfos, ref string message)
		{
			Result result;
			try
			{
				result = new GeneralTableOutputKit(cmdData).Drawing(tableInfos, ref message, null, true);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "提示");
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result DrawGeneralTable(ExternalCommandData cmdData, int rowCount, int colCount, List<CellInfo> cellInfos, ref string message, List<double> rowHeightList = null, List<double> colWidthList = null, bool drag = true)
		{
			TableInfos tableInfos = GeneralTableCreator.CreateTableInfos(cmdData.Application.ActiveUIDocument.Document, rowCount, colCount, cellInfos, rowHeightList, colWidthList);
			Result result;
			try
			{
				result = new GeneralTableOutputKit(cmdData).Drawing(tableInfos, ref message, null, drag);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result ExportGeneralTable(ExternalCommandData cmdData, TableInfos tableInfos, ref string message)
		{
			Result result;
			try
			{
				result = new GeneralTableOutputKit(cmdData).Export(tableInfos, ref message);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "提示");
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result ExportGeneralTable(ExternalCommandData cmdData, int rowCount, int colCount, List<CellInfo> cellInfos, ref string message, List<double> rowHeightList = null, List<double> colWidthList = null)
		{
			TableInfos tableInfos = GeneralTableCreator.CreateTableInfos(cmdData.Application.ActiveUIDocument.Document, rowCount, colCount, cellInfos, rowHeightList, colWidthList);
			Result result;
			try
			{
				result = new GeneralTableOutputKit(cmdData).Export(tableInfos, ref message);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static Result DrawGeneralTable(ExternalCommandData cmdData, int rowCount, int colCount, List<CellInfo> cellInfos, Autodesk.Revit.DB.View view, TextNoteType textStyle, ref string message, List<double> rowHeightList = null, List<double> colWidthList = null, bool drag = false)
		{
			TableInfos tableInfos = GeneralTableCreator.CreateTableInfos(cmdData.Application.ActiveUIDocument.Document, rowCount, colCount, cellInfos, rowHeightList, colWidthList);
			Result result;
			try
			{
				GeneralTableOutputKit generalTableOutputKit = new GeneralTableOutputKit(cmdData);
				tableInfos.FontSetting = textStyle;
				result = generalTableOutputKit.Drawing(tableInfos, ref message, view, drag);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "提示");
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public static List<MaterialProgramInfos.BasicInfo> GetProgramInfoList(SubjectType subjectType, bool isExcelTemplate, ExternalCommandData cmdData)
		{
			List<MaterialProgramInfos.BasicInfo> list = new List<MaterialProgramInfos.BasicInfo>();
			List<MaterialProgramInfos.BasicInfo> result;
			try
			{
				list = new MaterialTableDesigner(cmdData).GetProgramInfoList(subjectType, isExcelTemplate);
				result = list;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = list;
			}
			return result;
		}

		public static void RemoveProgram(SubjectType subjectType, string programName, string programId, ExternalCommandData cmdData)
		{
			try
			{
				new MaterialTableDesigner(cmdData).RemoveProgram(subjectType, programId);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
			}
		}

		public static Result GetStatisticsDetailsWithMaterialTable(SubjectType subjectType, ExternalCommandData cmdData, string programName, string programId, ref List<string> selectedCategoryNameList, ref List<CombinationOfAttributes> combinationOfAttributesList, ref List<MaterialProgramInfos.FilterType> filterTypeList, ref string message)
		{
			Result result;
			try
			{
				if (new MaterialTableDesigner(cmdData).GetStatisticsDetailsWithMaterialTable(subjectType, cmdData, programId, ref selectedCategoryNameList, ref combinationOfAttributesList, ref filterTypeList, ref message))
				{
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				else
				{
					result = Autodesk.Revit.UI.Result.Failed;
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		[Obsolete("该接口在3.0已废弃，请选择AddMaterialTableProgram或者EditMaterialTableProgram接口进行材料表定义.")]
		public static Result MaterialTableSetting(ExternalCommandData cmdData, ref string message, SubjectType subjectType, List<string> subjectFieldList)
		{
			message = "该接口已废弃";
			return Autodesk.Revit.UI.Result.Failed;
		}

		[Obsolete("该接口在3.0已废弃，请选择AddMaterialTableProgram或者EditMaterialTableProgram接口进行材料表定义.")]
		public static Result MaterialTableSetting(ExternalCommandData cmdData, ref string message, SubjectType subjectType, List<string> subjectFieldList, ref List<CombinationOfAttributes> userSelectSubjectFieldList)
		{
			message = "该接口已废弃";
			return Autodesk.Revit.UI.Result.Failed;
		}

		public static Result SwitchCableTrenchSectionView(ExternalCommandData cmdData)
		{
			if (new ViewControler(cmdData, ViewControler.TableViewType.TVT_CABLETRENCHSECTIONVIEW).CreateDraftingView(100) == ViewSelectResult.VSR_Cancel)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result SwitchRampExpandedView(ExternalCommandData cmdData)
		{
			if (new ViewControler(cmdData, ViewControler.TableViewType.TVT_RampExpandedView).CreateDraftingView(100) == ViewSelectResult.VSR_Cancel)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static ViewSelectResult GridDrawView(ExternalCommandData cmdData, string GridName = "表格工具")
		{
			return new ViewControler(cmdData, ViewControler.TableViewType.TVT_GRIDDRAW, GridName).CreateDraftingView(100);
		}

		public static Result GridNewDrawView(ExternalCommandData cmdData, string GridName = "表格工具")
		{
			if (!new ViewControler(cmdData, ViewControler.TableViewType.TVT_GRIDDRAW, GridName).CreateNewDraftingView(100))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
